<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<script type="text/javascript">
var patientId = $("#patientId").val();
var visitId = $("#visitId").val();
function page(n, s) {
    $("#pageNo1").val(n);
    $("#pageSize1").val(s);
    $("#ordersSearchForm").submit();
    return false;
}
$(function () {
    $.ajaxSetup({ cache: false });
    $("#btnSubmit").on('click', function () {
        addButtonStyle("btnSubmit");
        $("#ordersSearchForm").submit();
    });

    //传输
    $("#sport").on('click', function () {
        addButtonStyle("sport");
        var ordersStatus = '1';
        updateStatus(ordersStatus);
    })
    //停止
    $("#stop").on('click', function () {
        addButtonStyle("stop");
        var ordersStatus = '8';
        updateStatus(ordersStatus);
    })
    //删除
    $("#delBtn").on('click', function () {
        addButtonStyle("delBtn");
        var ordersStatus = "";
        updateStatus(ordersStatus);
    })
    //修改医嘱状态
    function updateStatus(ordersStatus) {
        var ordersIds = getSelectOrderIds(ordersStatus);//以逗号分隔
        if (ordersStatus == "") {
            if (ordersIds.length > 1) {
                confirmExtend('确认要删除该医嘱吗？', function () {
                    return delLoad("rigthDoctorCenterDiv", '${ctx}/orders/orders/delete?id=' + ordersIds + "&orderStatus=" + ordersStatus, '${ctx}/orders/orders/index?patientId=' + patientId + "&visitId=" + visitId);
                });
            }
        } else {
            if (ordersIds.length > 1) {
                return delLoad("rigthDoctorCenterDiv", '${ctx}/orders/orders/delete?id=' + ordersIds + "&orderStatus=" + ordersStatus, '${ctx}/orders/orders/index?patientId=' + patientId + "&visitId=" + visitId);
            }
        }
    }

    //弹出右
    $("#addClinic").on('click', function () {
        addButtonStyle("addClinic");
        var billingAttr = $("#billingAttr").val();
        var isDrug = $("#isDrug").val();
        var repeatIncaditor = $("#repeatIncaditor1").val();
        if (repeatIncaditor == '') {
            toastr.error("请选择持续分类！");
            return false;
        }
        if (isDrug == '') {
            toastr.error("请选择持续类别！");
            return false;
        }
        if (billingAttr == '') {
            toastr.error("请选择计价属性！");
            return false;
        }
        $.ajax({
            type: 'get',
            cache: false,
            url: '${ctx}/orders/orders/findByUser?valuation=' + billingAttr + '&category=' + isDrug + '&continue1=' + repeatIncaditor,
            success: function (data) {
                if (data.length > 0) {
                    loadRight(billingAttr, isDrug, repeatIncaditor, '', '');
                } else {
                    toastr.error("参数选择错误");
                }
            }
        });

    })
    $("#addChild").on('click', function () {
        addButtonStyle("addChild");
        var billingAttr = $("#billingAttr").val();
        var isDrug = $("#isDrug").val();
        var repeatIncaditor = $("#repeatIncaditor1").val();
        var parentId = getSelectOrderIds("z");
        if (parentId.length > 1) {
            loadRight(billingAttr, isDrug, repeatIncaditor, parentId, '');
        }
    })
    $("#updateBtn").on('click', function () {
        addButtonStyle("updateBtn");
        var billingAttr = $("#billingAttr").val();
        var isDrug = $("#isDrug").val();
        var repeatIncaditor = $("#repeatIncaditor1").val();
        var ordersId = getSelectOrderIds("x");
        if (ordersId.length > 1) {
            loadRight(billingAttr, isDrug, repeatIncaditor, '', ordersId);
        }
    })
    /**
     * 加载右侧数据
     * @param parentId
     * @returns {*}
     */
    function loadRight(billingAttr, isDrug, repeatIncaditor, parentId, ordersId) {
        hideShowTableTd('contentTable', '5,6,7,8,9,10', '2');
        $("#LeftDiv").attr("class", "main-content col-lg-7");
        $("#RightDiv").show('normal');
        if (ordersId != '' && ordersId != null) {
            return loadDiv('rigthCenterDiv', timestamp('${ctx}/orders/orders/updateForm?patientId=' + patientId + "&visitId=" + visitId + '&billingAttr=' + billingAttr + '&isDrug=' + isDrug + '&repeatIncaditor=' + repeatIncaditor + '&id=' + ordersId));
        } else {
            return loadDiv('rigthCenterDiv', timestamp('${ctx}/orders/orders/updateForm?patientId=' + patientId + "&visitId=" + visitId + '&billingAttr=' + billingAttr + '&isDrug=' + isDrug + '&repeatIncaditor=' + repeatIncaditor + '&parent.id=' + parentId));
        }
    }

    /**
     *  ordersStatus 1 :传输医嘱只能传输新建医嘱
     * @param ordersStatus
     * @returns {string}
     */
    function getSelectOrderIds(ordersStatus) {
        // 修改只能是一条  :新建的或者是驳回的
        // 删除：新建的或者是驳回的
        //子医嘱： 新建的一条
        //停止： 执行的长期
        //传输：新建的

        var parentOrders = $("#contentTable input[name=ordersId]:checked");
        if (parentOrders.length == 0) {
            toastr.error("请先选择一条医嘱！");
            return "";
        }
        if ((ordersStatus == "z" || ordersStatus == "x") && parentOrders.length != 1) {
            toastr.error("只能选择一条医嘱！");
            return "";
        }
        var ordersId = "";
        parentOrders.each(function () {
            var os = $(this).val().split(',')[2];
            var re = $(this).val().split(',')[4];
            if (ordersStatus == "8" && os != "6") {
                toastr.error("该医嘱不能停止！");
                return "";
            }
            if(ordersStatus == "8"&&re=="0"){
                toastr.error("该医嘱不能停止！");
                return "";
            }
            if (ordersStatus == "x" && (os != "0" && os != "7")) {
                toastr.error("该医嘱不能修改！");
                return "";
            }
            if (ordersStatus == "" && (os != "0" && os != "7")) {
                toastr.error("该医嘱不能删除！");
                return "";
            }
            if (ordersStatus == "z" && (os != "0")) {
                toastr.error("该医嘱不能添加子医嘱！");
                return "";
            }
            if (ordersStatus == "1" && (os != "0")) {
                toastr.error("该医嘱不能传输！");
                return "";
            }
            if ($(this).val().split(',')[1] == '1' && ordersStatus != "1") {
                toastr.error("请在申请处进行操作！");
                return "";
            }
            ordersId = ordersId + $(this).val().split(',')[0] + ",";
        });
        ordersId = ordersId.substring(0, ordersId.length - 1);
        return ordersId;
    }

    $("#isDrug").change(function () {
        var isDrug = $(this).val();
        if (isDrug == 2) {
            $("#billingAttr option[value=0]").attr("selected", "selected");
            $("#billingAttr").change();
        }
        if (isDrug == 1) {
            $("#billingAttr option[value=1]").attr("selected", "selected");
            $("#billingAttr").change();
        }

    })
    //全选事件
    $("#all").on('click', function () {
        var isChecked = $(this).prop("checked");
        $("input[name='ordersId']").prop("checked", isChecked);
    })
    $("#contentTable").treeTable({expandLevel: 10});
})
//删除子
function delChild(id, os) {
    if (os != '0') {
        toastr.error("不能删除非新建的医嘱！");
        return "";
    }
    confirmExtend('确认要删除该子医嘱吗？', function () {
        delLoad('rigthDoctorCenterDiv', "${ctx}/orders/orders/delete?id=" + id + "&orderStatus=''", '${ctx}/orders/orders/index?patientId=' + patientId + "&visitId=" + visitId);
    });
}


function addTemplate(projectType, creator, deptCode, patientId, visitId) {
    addButtonStyle("template");
    layer.open({
        type: 2,
        title: '处方历史查询',
        area: ['1000px', '650px'],
        fixed: false, //不固定
        maxmin: true,
        content: ctx + '/doctor/outpTreatRec/indexTreat?type=' + projectType + '&creator=' + creator + '&deptCode=' + deptCode + '&visitId=' + visitId + '&patientId=' + patientId
    });
}
//打印
function print1(patientId, visitId) {
    addButtonStyle("print");
    var isDrug = $("#isDrug").val();
    var repeatIncaditor = $("#repeatIncaditor1").val();
    if (isDrug.length == 0) {
        toastr.error("请先选择类别！");
        return false;
    }
    if (repeatIncaditor.length == 0) {
        toastr.error("请选择持续分类！");
        return false;
    }
    printPdf('${ctx}/orders/ordersConf/temporaryMedicalLabelPrint', 'isDrug=' + isDrug + '&repeatIncaditor=' + repeatIncaditor + '&patientId=' + patientId + '&visitId=' + visitId);
}
function addButtonStyle(inputId) {
    $("input[name='btn']").each(function () {
        $(this).attr("class", "btn btn-default");
    });
    $("#" + inputId).attr("class", "btn btn-primary");
}
</script>
<div class="app">
    <section class="layout">
        <section class="main-content col-lg-12" id="LeftDiv">
            <div class="content-wrap">
                <div class="wrapper" style="bottom: 50px;">
                    <section class="panel panel-default">
                        <div class="panel-body">
                            <div class="form-group">
                                <input name="btn" class="btn btn-default" type="button" value="新增"  id="addClinic"/>
                                <input name="btn" class="btn btn-default" type="button" value="子医嘱" id="addChild"    />
                                <input name="btn" class="btn btn-default" type="button" value="传输" id="sport"/>
                                <input name="btn" class="btn btn-default" type="button" value="修改" id="updateBtn" />
                                <input name="btn" class="btn btn-default" type="button" value="删除" id="delBtn" />
                                <input name="btn" class="btn btn-default" type="button" value="停止" id="stop"/>
                                <input name="btn" class="btn btn-default" type="button" value="打印" id="print"
                                       onclick="print1('${orders.patientId}','${orders.visitId}')"
                                        />
                                <input id="btnSubmit" class="btn btn-default" type="button" value="查询"  />
                                <input name="btn" class="btn btn-default" type="button" value="模板" id="template"
                                       onclick="addTemplate('1','${creator}','${billingDept}','${orders.patientId}','${orders.visitId}')"  />
                            </div>
                            <div class="table-responsive no-border">
                                <div class="datatable-top">
                                    <div class="pull-left">
                                        <form:form id="ordersSearchForm" modelAttribute="orders"
                                                   onsubmit="return loadDivForm('rigthDoctorCenterDiv','ordersSearchForm','${ctx}/orders/orders/index');"
                                                   method="post" class="form-inline">
                                            <input id="patientId" name="patientId" type="hidden"
                                                   value="${orders.patientId}"/>
                                            <input id="visitId" name="visitId" type="hidden" value="${orders.visitId}"/>
                                            <input id="pageNo1" name="pageNo" type="hidden"
                                                   value="${ordersPage.pageNo}"/>
                                            <input id="pageSize1" name="pageSize" type="hidden"
                                                   value="${ordersPage.pageSize}"/>
                                            <%--<input class="btn btn-primary" type="button" value="新增选择->"--%>
                                                   <%--style="background-color:green;" disabled="disabled"/>--%>
                                            <form:select path="" id="repeatIncaditor1"
                                                         class="form-control" itemLabel="label" itemValue="value">
                                                <form:option value="1" label="长期"/>
                                                <form:option value="0" label="临时"/>
                                            </form:select>
                                            <form:select itemLabel="label" itemValue="value" id="isDrug" path=""
                                                         class="form-control" data-parsley-required="true">
                                                <form:option value="0" label="药品"/>
                                                <form:option value="1" label="非药品"/>
                                                <form:option value="2" label="嘱托类"/>
                                            </form:select>
                                            <form:select path="" itemLabel="label" itemValue="value" id="billingAttr"
                                                         class="form-control" data-parsley-required="true">
                                                <form:options items="${fns:getDictList('BILLING_ATTR')}"
                                                              itemLabel="label"
                                                              itemValue="value" htmlEscape="false"/>
                                            </form:select>
                                            <%--<input class="btn btn-primary" type="button" value="查询条件"--%>
                                                   <%--style="background-color:	#73BF00;" disabled="disabled"/>--%>
                                            <form:select path="orderStatus" itemLabel="label" itemValue="value"
                                                         id="orderStatus" class="form-control"
                                                         data-parsley-required="true">
                                                <form:option value="" label="医嘱状态"/>
                                                <form:option value="0" label="新开"/>
                                                <form:option value="1" label="传输"/>
                                                <form:option value="2" label="转抄"/>
                                                <form:option value="3" label="临时执行"/>
                                                <form:option value="4" label="停止"/>
                                                <form:option value="6" label="执行"/>
                                                <form:option value="7" label="护士驳回"/>
                                                <form:option value="7" label="护士驳回"/>
                                                <form:option value="8" label="医生停止"/>
                                            </form:select>
                                        </form:form>
                                    </div>
                                </div>
                                <table id="contentTable" class="table table-bordered mg-t datatable">
                                    <thead>
                                    <tr>
                                        <th><input type=checkbox id="all"></th>
                                        <th>下达时间</th>
                                        <th>医嘱内容</th>
                                        <th>状态</th>
                                        <th>长期</th>
                                        <th>途径</th>
                                        <th>频次</th>
                                        <th>执行时间</th>
                                        <th>计价</th>
                                        <th>每次用量</th>
                                        <th>执行科室</th>
                                        <th>医生停止时间</th>
                                        <th>护士停止时间</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <c:forEach items="${ordersPage.list}" var="orders" varStatus="ss">
                                        <tr id="${orders.id}" style="text-align: center;">
                                            <td><input type=checkbox name="ordersId"
                                                       value="${orders.id},${orders.isApply},${orders.orderStatus},${orders.isChild},${orders.repeatIncaditor}">
                                            </td>
                                            <td><fmt:formatDate value="${orders.createDate}"
                                                                pattern="yyyy-MM-dd HH:mm"/></td>
                                            <td> ${orders.orderText}</td>
                                            <td> ${orders.orderStatusTran}</td>
                                            <td> ${orders.repeatIncaditorTran}</td>
                                            <td> ${orders.administration}</td>
                                            <td> ${orders.performFreqDict.freqDesc}</td>
                                            <td> ${orders.nursePerformTime}</td>
                                            <td> ${orders.billingAttr}</td>
                                            <td>
                                                <c:if test="${orders.isDrugTran=='药品'}">
                                                    ${orders.singleDose}( ${orders.doseUnits})
                                                </c:if>
                                            </td>
                                            <td> ${fns:getDeptName(orders.performedBy,'')} </td>
                                            <td><fmt:formatDate value="${orders.stopInDate}"
                                                                pattern="yyyy-MM-dd HH:mm"/></td>
                                            <td><fmt:formatDate value="${orders.nurseStopTime}"
                                                                pattern="yyyy-MM-dd HH:mm"/></td>
                                        </tr>
                                        <c:if test="${orders.children.size()>0}">
                                            <c:forEach items="${orders.children}" var="child" varStatus="ss">
                                                <tr id='${ss.count}' pId='${orders.id}' style="text-align: center;">
                                                    <td>
                                                        <c:if test="${child.orderStatus==0 ||child.orderStatus==7 }">
                                                            <a href="#" style="margin-top: 0px;"
                                                               class="applyBt btn btn-danger btn-xs"
                                                               onclick="delChild('${child.id}','${child.orderStatus}')">删除</a>
                                                        </c:if>
                                                    </td>
                                                    <td><fmt:formatDate value="${child.createDate}"
                                                                        pattern="yyyy-MM-dd HH:mm"/></td>
                                                    <td> ${child.orderText}</td>
                                                    <td> ${child.orderStatusTran}</td>
                                                    <td> ${child.repeatIncaditorTran}</td>
                                                    <td> ${orders.administration}</td>
                                                    <td> ${orders.performFreqDict.freqDesc}</td>
                                                    <td> ${child.nursePerformTime}</td>
                                                    <td> ${child.billingAttr}</td>
                                                    <td>
                                                        <c:if test="${child.isDrugTran=='药品'}">
                                                            ${child.singleDose}( ${child.doseUnits})
                                                        </c:if>
                                                    </td>
                                                    <td> ${fns:getDeptName(child.performedBy,'')} </td>
                                                    <td><fmt:formatDate value="${child.stopInDate}"
                                                                        pattern="yyyy-MM-dd HH:mm"/></td>
                                                    <td><fmt:formatDate value="${child.nurseStopTime}"
                                                                        pattern="yyyy-MM-dd HH:mm"/></td>
                                                </tr>
                                            </c:forEach>
                                        </c:if>
                                    </c:forEach>
                                    </tbody>
                                </table>
                                <div class="pagination">${ordersPage}</div>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>
        <%--右侧诊疗项目列表--%>
        <section class="main-content col-lg-5" style="display: none" id="RightDiv">
            <div class="content-wrap" id="rigthCenterDiv">
            </div>
        </section>
    </section>
</div>

